﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

using AttributeRouting;
using AttributeRouting.Web.Mvc;
using Newtonsoft.Json.Linq;

using LuxDiner.Model.Results;
using LuxDiner.Model.Financial;
using LuxDiner.Logic.Financial;
namespace LuxDiner.Web.Management.Areas.Financial.Controllers
{
    [RouteArea("Financial")]
    [RoutePrefix("Bank")]
    public class BankApiController : ApiController
    {
        [Route("Gets")]
        [HttpPost]
        public JsonEntitySet<BankModel> Gets(JObject request)
        {
            string errinfo = "no data";
            try
            {
                var result = BankLogic.GetBanks();
                return new JsonEntitySet<BankModel>(result);
            }
            catch (Utils.LuxDinerException lde)
            {
                errinfo = lde.Message;
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonEntitySet<BankModel>(null, errinfo, false);
        }

        [Route("Save")]
        [HttpPost]
        public JsonEntity<BankModel> Save(JObject request)
        {
            string errinfo = "对不起！保存失败，出错原因：";
            try
            {
                #region 收集参数
                BankModel model = new BankModel() {};
                model.ID = request.Value<int>("bankID");
                model.BankName = request.Value<string>("bankName");
                model.CardNo = request.Value<string>("cardNo");
                #endregion

                if (model.ID == 0)
                    model = BankLogic.CreateNew(model);
                else
                    BankLogic.Update(model);
                return new JsonEntity<BankModel>(model);
            }
            catch (Utils.LuxDinerException lde)
            {
                errinfo += lde.Message;
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonEntity<BankModel>(null, errinfo, false);
        }

        [Route("Delete")]
        [HttpPost]
        public JsonResult<bool> Delete(JObject request)
        {
            string errinfo = "对不起！删除失败，出错原因：";
            try
            {
                #region 收集参数
                var id = request.Value<int>("bankID");
                #endregion
                BankLogic.Delete(id);
                return new JsonResult<bool>(true);
            }
            catch (Utils.LuxDinerException lde)
            {
                errinfo += lde.Message;
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonResult<bool>(false, errinfo, false);
        }
    }
}
